using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.Design.WebControls;
using System.ComponentModel;


[Designer(typeof(CompositeControlDesigner))]
public partial class SearchControl : System.Web.UI.UserControl
{
    protected bool uAutoPostBack = false;
    [Bindable(true),
        Category("Behavior"),
        DefaultValue(false),
        Description("Automatically postback to server after searching is done"),
        Localizable(true)]
    public bool AutoPostBack
    {
        get { return uAutoPostBack; }
        set { uAutoPostBack = value; }
    }

    protected SearchType uType = SearchType.SearchContact;
    [Bindable(true),
        Category("Behavior"),
        DefaultValue(SearchType.SearchContact),
        Description("Set the type of searching used"),
        Localizable(true)]
    public SearchType Type
    {
        get { return uType; }
        set { uType = value; }
    }

    protected int uMinLength = 3;
    [Bindable(true),
        Category("Behavior"),
        DefaultValue(3),
        Description("Minimal Length for Searching"),
        Localizable(true)]
    public int MinLength
    {
        get { return uMinLength; }
        set { uMinLength = value; }
    }

    protected string uMinLengthAlert = "Harap ketik minimal karakter untuk memulai pencarian.";
    [Bindable(true),
        Category("Behavior"),
       DefaultValue("Harap ketik minimal karakter untuk memulai pencarian."),
        Description("Alert for Searching less than Minimal Length"),
        Localizable(true)]
    public string MinLengthAlert
    {
        get { return uMinLengthAlert; }
        set { uMinLengthAlert = value; }
    }

    [Bindable(true),
       Category("Appearance"),
       DefaultValue("Klik disini untuk mencari nama kota"),
       Description("ToolTip for Search Button"),
       Localizable(true)]
    public string ButtonToolTip
    {
        get { return btnSearch.ToolTip; }
        set { btnSearch.ToolTip = value; }
    }

    [Bindable(true),
       Category("Appearance"),
       DefaultValue("Cari Data"),
       Description("Text for Search Button"),
       Localizable(true)]
    public string ButtonText
    {
        get { return btnSearch.Text; }
        set { btnSearch.Text = value; }
    }

    protected ReturnType uReturnType = ReturnType.ValueOnly;
    [Bindable(true),
       Category("Behavior"),
       DefaultValue(ReturnType.ValueOnly),
       Description("Value returned from search form to be displayed in search textbox"),
       Localizable(true)]
    public ReturnType ReturnType
    {
        get { return uReturnType; }
        set { uReturnType = value; }
    }

    [Category("Layout"), Description("The width of text box control"), Localizable(true)    
    ]
    public Unit TextBoxWidth
    {
        get{ return  (Unit) this.txtSearch.Width ; }
        set {
            this.txtSearch.Width = value; 
        }
    }

    [Category("Layout"), Description("The height of text box control"), Localizable(true)
    ]
    public Unit TextBoxHeight
    {
        get { return (Unit)this.txtSearch.Height; }
        set
        {
            this.txtSearch.Height = value;
        }
    }

    [Category("Behavior"), Description("Max length of text box control"), Localizable(true)
    ]
    public int MaxLength
    {
        get { return this.txtSearch.MaxLength; }
        set
        {
            this.txtSearch.MaxLength = value;
        }
    }

    [Category("Behavior"), Description("Condition"), Localizable(true)
    ]
    protected string uCondition = "";
    public string Condition
    {
        get { return uCondition; }
        set
        {
            uCondition = value;
        }
    }
    [Category("Layout"), WebDescription("The width of button control"), WebBrowsable(true)
    ]
    public Unit ButtonWidth
    {
        get { return (Unit) this.btnSearch.Width; }
        set
        {
            Unit NewUnit = value;
            this.btnSearch.Width = NewUnit;
        }
    }

    [Category("Appearance"), Description("The value of search control"), Localizable(true)
    ]
    public string Value
    {
        get { return this.hidSearch.Value; }
        set
        {
            this.hidSearch.Value = value;
        }
    }

    [Category("Appearance"), Description("The text of search control"), Localizable(true)
    ]
    public string Text
    {
        get { return this.txtSearch.Text; }
        set
        {
            this.txtSearch.Text = value;
        }
    }
    
    public void Clear()
    {
        this.txtSearch.Text = "";
        this.hidSearch.Value = "";
    }

	[DefaultValue(true),
        Category("Appearance"), Description("The enabled of search control")
    ]
    public bool SearchEnable
    {
        get
        {
            return this.btnSearch.Enabled;
        }
        set
        {
            this.btnSearch.Enabled = value;
            this.txtSearch.Enabled = value;
            if (value == false)
            {
                this.btnSearch.BackColor = System.Drawing.SystemColors.InactiveCaption;
            }
            else
            {
                this.btnSearch.BackColor = System.Drawing.Color.Maroon;
            }
        }
    }

    [DefaultValue(true),
        Category("Appearance"), Description("The visible of search button control")
    ]
    public bool ButtonVisible
    {
        get
        {
            return this.btnSearch.Visible;
        }
        set
        {
            this.btnSearch.Visible = value;
        }
    }

    [DefaultValue(true),
            Category("Appearance"), Description("The enabled of search text control")
        ]
    public bool TextBoxEnable
    {
        get
        {
            return this.txtSearch.Enabled;
        }
        set
        {
            this.txtSearch.Enabled = value;
        }
    }

    [DefaultValue(true),
            Category("Appearance"), Description("The enabled of search button control")
        ]
    public bool ButtonEnable
    {
        get
        {
            return this.btnSearch.Enabled;
        }
        set
        {
            this.btnSearch.Enabled = value;
            if (value == false)
            {
                this.btnSearch.BackColor = System.Drawing.SystemColors.InactiveCaption;
            }
            else
            {
                this.btnSearch.BackColor = System.Drawing.Color.Maroon;
            }
        }
    }

    public event System.EventHandler ButtonClicked; 

    protected void Page_Load(object sender, EventArgs e)
    {
            string strScript = "<script language='javascript' type='text/javascript'>\n" +
             
            "function doSearch(val, id, type, minLength, minLengthAlert, returnType, dataSource, condition, autoPostBack, btnSearchID) {\n" +
            "   if(val.value.length >= minLength) {\n" +
            "       var retVal = new Array(val.value, id.value);\n" +
            "       if (window.showModalDialog) {\n" +
            "           retVal = window.showModalDialog('../../control/PageSearch.aspx?q=' + retVal[0] + '&s=' + type + '&c=' + returnType + '&ds=' + dataSource + '&x=' + condition + '&r=" + Guid.NewGuid().ToString() + "',null,'dialogHeight:600px;dialogWidth:800px');\n" +
            "       } else {\n" +
            "           window.retVal = val;\n" +
            "           window.retID = id;\n" +
            "           window.postBack = autoPostBack;\n" +
            "           window.objBtn = btnSearchID;\n" +
            "           window.open('../../control/PageSearch.aspx?q=' + retVal[0] + '&s=' + type + '&c=' + returnType + '&ds=' + dataSource + '&x=' + condition + '&r=" + Guid.NewGuid().ToString() + "','" + Guid.NewGuid().ToString() + "','scrollbars=yes, width=800, height=600, left=0, top=0, dependent=yes, modal=yes');\n" +
            "           return false;\n" +
            "       }\n" +
            "       if (retVal[0]!=undefined) {\n" +
            "           if (returnType=='CodeOnly') {\n" +
            "               val.value = unescape(retVal[1]);\n" +
            "           } else if (returnType=='ValueOnly') {\n" +
            "               val.value = unescape(retVal[0]);\n" +
            "           } else {\n" +
                //"               val.value = unescape(retVal[1]) + '-' + unescape(retVal[0]);\n" +
            "               val.value = unescape(retVal[0]);\n" +
            "           }\n" +
            "           id.value = unescape(retVal[1]);\n" +
            "           return true;\n" +
            "       } else {\n" +
            "           return false;\n" +
            "       }\n" +
            "   } else {\n" +
            "       alert(minLengthAlert);\n" +
            "       return false;\n" +
            "   }\n" +
            "}\n" +
            "function PostBackOnMainPage(btnSearch){\n" +
            "   __doPostBack(btnSearch,'');\n" +
            "}\n" +
            "</script>";

        if (!Page.ClientScript.IsStartupScriptRegistered("Startup"))
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);
        
        uMinLengthAlert = String.Format("Harap ketik minimal {0} karakter untuk memulai pencarian.", uMinLength);
        if (uAutoPostBack)
        {
            btnSearch.Attributes.Add("OnClick", String.Format("return doSearch(document.forms[0].{0}, document.forms[0].{1}, '{2}',{3},'{4}', '{5}', '{6}', true, '{7}');", txtSearch.ClientID, hidSearch.ClientID, uType, uMinLength, uMinLengthAlert, uReturnType, HttpUtility.UrlEncode(uCondition).Replace("'", "%27"), btnSearch.ClientID));
        }
        else
        {
            btnSearch.Attributes.Add("OnClick", String.Format("doSearch(document.forms[0].{0}, document.forms[0].{1}, '{2}',{3},'{4}', '{5}', '{6}', false, ''); return false;", txtSearch.ClientID, hidSearch.ClientID, uType, uMinLength, uMinLengthAlert, uReturnType, HttpUtility.UrlEncode(uCondition).Replace("'", "%27")));
        }
        txtSearch.Attributes.Add("OnChange", String.Format("document.forms[0].{0}.value = '';", hidSearch.ClientID));
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.ButtonClicked != null)
            this.ButtonClicked(sender, e);
    }
}
